package mk.company.fin.panel;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import mk.company.fin.AppConstants;
import mk.company.fin.frame.AbstractFrame;
import mk.company.fin.frame.FinFrame;
import mk.company.fin.frame.internal.AbstractInternalFrame;
import mk.company.fin.utils.AppUtils;

/**
 * Collapsible panel.
 * 
 * @author vlado.gjorgjiev
 */
public class CollapsiblePanel {

	private JLabel titleLabel;
	private JPanel panel;
	
	private Icon titleBackground;
	private Icon titleBackgroundOver;
	
	private Icon componentBackground;
	private Icon componentBackgroundOver;
	
	
	public CollapsiblePanel(JComponent container,
			Icon titleBackground, Icon titleBackgroundOver,
			Icon componentBackground, Icon componentBackgroundOver) {
		
		// sets
		this.titleBackground = titleBackground;
		this.titleBackgroundOver = titleBackgroundOver;
		this.componentBackground = componentBackground;
		this.componentBackgroundOver = componentBackgroundOver;
		
		// title
		titleLabel = new JLabel(titleBackground);
		titleLabel.setFont(AppConstants.Fonts.FONT_ARIAL_BOLD_12);
		titleLabel.setForeground(Color.WHITE);
		titleLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
		titleLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
		AppUtils.setStandardAlignmentAndPosition(titleLabel);
		createTitleActions();
		
		// panel
		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.setBackground(AppConstants.Colors.LIGHT_BLUE);
		panel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
		panel.setVisible(false);
		
		// show
		container.add(Box.createRigidArea(new Dimension(0, 7)));
		container.add(titleLabel);
		container.add(panel);
	}

	private void createTitleActions() {
		titleLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				titleLabel.setIcon(titleBackgroundOver);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				titleLabel.setIcon(titleBackground);
			}
			
			@Override
			public void mouseClicked(MouseEvent mouseevent) {
				if (panel.isVisible()) {
					panel.setVisible(false);
				} else {
					panel.setVisible(true);
				}
			}
		});
	}
	
	public void createComponent(final String text, final AbstractFrame sourceFrame, final AbstractInternalFrame targetFrame) {
		JLabel label = new JLabel(text);
		label.setFont(AppConstants.Fonts.FONT_ARIAL_11);
		label.setForeground(Color.BLACK);
		label.setCursor(new Cursor(Cursor.HAND_CURSOR));
		label.setIcon(componentBackground);
		label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
		AppUtils.setStandardAlignmentAndPosition(label);
		
		label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent mouseevent) {
				JLabel component = (JLabel) mouseevent.getComponent();
				component.setIcon(componentBackgroundOver);
			}
			
			@Override
			public void mouseExited(MouseEvent mouseevent) {
				JLabel component = (JLabel) mouseevent.getComponent();
				component.setIcon(componentBackground);
			}
			
			@Override
			public void mouseClicked(MouseEvent mouseevent) {
				targetFrame.setContext(sourceFrame.getContext());
				((FinFrame)sourceFrame).drawInternalFrame(targetFrame);
			}
		});
		panel.add(label);
	}

	
	public void setTitle(String title) {
		titleLabel.setText(title);
	}
	
	public void setTitleIcon(Icon icon) {
		titleLabel.setIcon(icon);
	}
	
	public JLabel getTitleLabel() {
		return titleLabel;
	}
	
	public JPanel getPanel() {
		return panel;
	}

}